package com.hzlx.config;

import com.hzlx.bean.Users;
import com.hzlx.dao.UserDao;
import com.hzlx.dto.UserDto;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class UserService {
    @Resource
    UserDao userDao;
    public UserDto findByUsername(String account) {
        Users users = userDao.loadByAccount(account);
        Stream<String> s1 = users.getPermissionList().stream()
                .map(perm -> perm.getPerm());
        Stream<String> s2 = users.getRoleList().stream().map(role-> "role:"+role.getRoleCode());
        List<String> authorities = Stream.concat(s1, s2)
                .filter(au->au!=null)
                .collect(Collectors.toList());
        return UserDto.builder()
                .username(users.getUsername())
                .account(users.getAccount())
                .password(users.getPassword())
                .authorities(authorities)
                .build();
    }
}
